package com.posti.ciudaddelbache.web.mvc.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.posti.ciudaddelbache.domain.beans.Bache;
import com.posti.ciudaddelbache.domain.service.BachesService;

public class BachesController extends MultiActionController {
  private BachesService bachesService;

  public ModelAndView list(HttpServletRequest request, HttpServletResponse response) {
    ModelAndView mv = new ModelAndView("list");
    List<Bache> baches = bachesService.getAllBaches();
    mv.addObject("baches", baches);
    return mv;
  }

  public ModelAndView save(HttpServletRequest request, HttpServletResponse response) {
    double lat = Double.parseDouble(request.getParameter("lat"));
    double lng = Double.parseDouble(request.getParameter("lng"));
    Bache bache = new Bache(lat, lng);
    bachesService.create(bache);
    ModelAndView mv = new ModelAndView("list");
    List<Bache> baches = bachesService.getAllBaches();
    mv.addObject("baches", baches);
    return mv;
  }

  /**
   * Sets the bachesService.
   * @param bachesService the bachesService to set
   */
  public void setBachesService(BachesService bachesService) {
    this.bachesService = bachesService;
  }
}
